const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const path = require('path')
const request = require('request')
const log4 = require('./tools/logs')
const config = require('./config/production')
const superagent = require('superagent')
const cheerio = require('cheerio')
require('body-parser-xml')(bodyParser)
app.use(bodyParser())
app.use(bodyParser.xml({
    limit: '2MB', // Reject payload bigger than 1 MB
    xmlParseOptions: {
        normalize: true, // Trim whitespace inside text nodes
        normalizeTags: true, // Transform tags to lowercase
        explicitArray: false // Only put nodes in array if >1
    }
}))
app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*")
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With")
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS")
    res.header("X-Powered-By", ' 3.2.1')
    if (req.method == "OPTIONS") res.send(200)
    else next()
})

app.use(express.static(path.join(__dirname, './static')))


function spider_list(cellback) {
    const reptileUrl = "https://www.oschina.net/blog?classification=428612"
    superagent.get(reptileUrl).end(function (err, res) {
        // 抛错拦截
        if (err) {
            console.log('false')
        }
        // 等待 code
        let $ = cheerio.load(res.text)
        //var cont = $('#mainScreen .container .web-blog .row .blog-list-wrap .tab article-list')
        var cont = $('#recommendArticleList .blog-list-container').children()
        var list_tle = []
        cont.each(function (i, elem) {
            var item = $(elem).find('.content')
            list_tle.push({
                tle: $(item).find('.header').text(),
                href: $(item).find('.header').attr('href'),
                description: $(item).find('.description .line-clamp').text()
            })
            // console.log(list_tle)
        })
        cellback(list_tle)
    })
}

function spider_artclecontent(page_url,cellback) {
    superagent.get(page_url).end(function (err, res) {
        // 抛错拦截
        if (err) {
            console.log('false')
        }
        // 等待 code
        let $ = cheerio.load(res.text)
        //var cont = $('#mainScreen .container .web-blog .row .blog-list-wrap .tab article-list')
        var cont = $('#articleContent').html()
        cellback(cont)
    })
}

app.post('/api/v3/get_artcle', function (req, res) {
    spider_list(function (data) {
        res.send(data)
    })
})

app.post('/api/v3/get_content', function (req, res) {
    //var url = 'https://my.oschina.net/u/3970421/blog/2967059'
    var url = req.body.url
    spider_artclecontent(url,function (data) {
        //var html = '<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0"><meta charset="UTF-8"></head> <body>'+data+'</body></html>'
        res.send(data)
    })
})

app.listen(config.port)
log4.logs('程序运行在' + config.port)